<?php
    require_once('popup_base_jectly.php');
	require_once('includes/paypal/CallerService.php');
	
	class masspay extends popup_base_jectly 
	{
		public $textbox_paypal_email;
		public $textbox_paypal_amount;
		public $lst_paypal_currency;
        public $textbox_delivery_within;
        public $textbox_comment;
        public $button_confirm;
        public $button_cancel;
        public $button_back;
        public $button_ok;
        public $nvpstr;
        public $resArray;
        public $location;
        public $receiverEmail;
        public $amount;
        public $uniqueID;
        public $noteEMAILSUBJECT;
        public $emailSubject;
        public $receiverType;
        public $currency;
        public $j;
        public $note;
        public $ack;
        //flags
        public $bool_confirming=false;
        public $bool_message_exist=false;
        public $bool_error=false;
        public $bool_permitted=false;
        //messages
        public $str_confirming_text='Are you sure to do this?<br /><br />';
        public $str_message;
        public $str_permission_error_text='no permission';
        
		 function __construct() {
            parent::__construct();
        }
          protected function Form_Create() {
          	  $this->check_query_string();
          	   $this->load_project();
          	   
          	   if($this->bool_bid_permitted())
            {
                $this->bool_permitted=true;
            }

            if($this->bool_permitted)
            {
                $this->textbox_paypal_email_Create();
                $this->textbox_paypal_amount_Create();
                $this->lst_paypal_currency_Create();
                $this->textbox_comment_Create();
                $this->button_confirm_Create();
                $this->button_cancel_Create();
                $this->button_back_Create();
                $this->button_ok_Create();
            }
            
		}
		protected function textbox_paypal_email_Create() {
            $this->textbox_paypal_email = new QTextBox($this) ;
            $this->textbox_paypal_email ->MaxLength=50;
            $this->textbox_paypal_email ->Width=200;
            $this->textbox_paypal_email ->Name = 'Pay';
            $this->textbox_paypal_email ->CssClass = 'input';
            //if($this->bool_confirming)$this->textbox_paypal_email ->Visible=false;
        }
        protected function textbox_paypal_amount_Create() {
            $this->textbox_paypal_amount = new QTextBox($this);
            $this->textbox_paypal_amount->MaxLength=20;
            $this->textbox_paypal_amount->Width=60;
            $this->textbox_paypal_amount->Name = 'Amount(*)';
            $this->textbox_paypal_amount->CssClass = 'input';
            //if($this->bool_confirming)$this->textbox_paypal_amount->Visible=false;
        }
         protected function lst_paypal_currency_Create() {
            $this->lst_paypal_currency = new QListBox($this);
           
            $this->lst_paypal_currency->Name = 'Amount(*)';
            $this->lst_paypal_currency->CssClass = 'input';
            $this->lst_paypal_currency->AddItem('--Select One--',null);
            $this->lst_paypal_currency->AddItem('USD','USD');
            $this->lst_paypal_currency->AddItem('GBP','GBP');
            $this->lst_paypal_currency->AddItem('EUR','EUR');
            $this->lst_paypal_currency->AddItem('JPY','JPY');
            $this->lst_paypal_currency->AddItem('CAD','CAD');
            $this->lst_paypal_currency->AddItem('AUD','AUD');
            //if($this->bool_confirming)$this->textbox_paypal_amount->Visible=false;
        }
        
            
          protected function textbox_comment_Create() {
            $this->textbox_comment = new QTextBox($this);
            $this->textbox_comment->Width=400;
            $this->textbox_comment->Columns=50;
            $this->textbox_comment->Rows=10;
            $this->textbox_comment->Name = 'comment';
            //$this->textbox_comment->CssClass = 'input';
            $this->textbox_comment->Visible=true;
            $this->textbox_comment->Wrap=true;
            $this->textbox_comment->TextMode=QTextMode::MultiLine;
            $this->textbox_comment->Overflow=QOverflow::Auto;
            if($this->bool_confirming)$this->textbox_comment->Visible=false;
        }
            protected function button_confirm_Create() {
            $this->button_confirm = new QButton($this);
            $this->button_confirm->Text = 'confirm';
            $this->button_confirm->AddAction(new QClickEvent(), new QServerAction('confirm_Click'));
            $this->button_confirm->PrimaryButton = true;
        }
          protected function button_cancel_Create() {
            $this->button_cancel = new QButton($this);
            $this->button_cancel->Text = 'cancel';
            $this->button_cancel->AddAction(new QClickEvent(), new QServerAction('cancel_Click'));
            //$this->button_cancel->PrimaryButton = true;
        }
        
         protected function button_back_Create() {
            $this->button_back = new QButton($this);
            $this->button_back->Text = 'back';
            $this->button_back->AddAction(new QClickEvent(), new QServerAction('back_Click'));
            //$this->button_back->PrimaryButton = true;
        }
        protected function button_ok_Create() {
            $this->button_ok = new QButton($this);
            $this->button_ok->Text = 'ok';
            $this->button_ok->AddAction(new QClickEvent(), new QServerAction('ok_Click'));
            //$this->button_ok->PrimaryButton = true;
        }

        public function confirm_Click($strFormId, $strControlId, $strParameter) {

            $this->bool_confirming=false;
            $this->bool_error=false;

            //check input
           if (isset($this->textbox_paypal_email->Text)) {

           	if (!$this->textbox_paypal_email->Validate())
           	{
           		  $this->textbox_paypal_email->Blink();
                $this->set_error_message('Please input valid Paypal Email');
           	      
            } 
           }
            if(!$this->bool_qcontrol_text_input_ok($this->textbox_paypal_amount))
            {
                //$this->append_line_confirming_text('textbox_delivery_within input='.$this->textbox_delivery_within->Text);
                //$this->bool_error=true;
                $this->textbox_paypal_amount->Blink();
                $this->set_error_message('Please input your delivery day(s)');
            }
            else
            {
                if(is_numeric($this->textbox_paypal_amount->Text))
                {
                    $amount=intval($this->textbox_paypal_amount->Text);

                    if($amount>=1 && $amount<=5000)
                    {

                    }
                    else
                    {
                        $this->textbox_paypal_amount->Blink();
                        $this->set_error_message('Please input your Amount within the range(0-5000)');
                    }
                }
                
            }

            if($this->bool_qcontrol_text_input_ok($this->textbox_comment))
            {
                //$this->append_line_confirming_text('textbox_delivery_within input='.$this->textbox_delivery_within->Text);
                //$this->bool_error=true;
                
                if($this->bool_qcontrol_text_input_within($this->textbox_comment,200) && $this->bool_qcontrol_text_input_over($this->textbox_comment,6))
                {
                    
                }
                else
                {
                    $this->textbox_comment->Blink();
                    $this->set_error_message('Please input your textbox_comment within the range(6-365)');
                }
            }

            //$this->reset_message();
            //$this->append_line_message('confirming');
            //$this->bool_message_exist=true;

            if(!$this->bool_error)
            {
                $this->append_line_confirming_text('paypal Email='.$this->textbox_paypal_email->Text);
                $this->append_line_confirming_text('Amount(*)='.$this->textbox_paypal_amount->Text);
                $this->append_line_confirming_text('comment='.$this->textbox_comment->Text);

                $this->bool_confirming=true;
                $this->bool_message_exist=false;
            }
            else
            {
                $this->bool_message_exist=true;
            }
        }

        public function cancel_Click($strFormId, $strControlId, $strParameter) {
            QApplication::ExecuteJavaScript('self.window.close();');
        }

        public function back_Click($strFormId, $strControlId, $strParameter) {
            $this->bool_confirming=false;
            $this->bool_message_exist=false;
        }

        public function ok_Click($strFormId, $strControlId, $strParameter) {

            
        	$this->emailSubject=urlencode($this->textbox_paypal_email->Text);
        	$this->receiverEmail=$this->textbox_paypal_email->Text;
        	$this->amount=$this->textbox_paypal_amount->Text;
        	$this->currency=$this->lst_paypal_currency->SelectedValue;
        	$this->receiverType='EnailAddress';
        	$this->emailSubject='You+have+the+money';
        	$this->j='0';
			
			
			
			//$count= count($_POST['receiveremail']);
			/*for($i=0,$j=0;$i<$count;$i++) {
					if (isset($_POST['receiveremail'][$i]) && $_POST['receiveremail'][$i]!='' ) {
							$receiverEmail = urlencode($_POST['receiveremail'][$i]);
							$amount = urlencode($_POST['amount'][$i]);
							$uniqueID = urlencode($_POST['uniqueID'][$i]);
							$note = urlencode($_POST['note'][$i]);
							$nvpstr.="&L_EMAIL$j=$receiverEmail&L_Amt$j=$amount&L_UNIQUEID$j=$uniqueID&L_NOTE$j=$note";
							$j++;
					}
			}*/
			/* Construct the request string that will be sent to PayPal.
			   The variable $nvpstr contains all the variables and is a
			   name value pair string with & as a delimiter */
			$this->nvpstr.="&L_EMAIL$this->j=$this->receiverEmail&L_Amt$this->j=$this->amount&L_UNIQUEID$this->j=$this->uniqueID&L_NOTE$this->j=$this->note";
			$this->nvpstr.="&EMAILSUBJECT=$this->emailSubject&RECEIVERTYPE=$this->receiverType&CURRENCYCODE=$this->currency" ;
			
			//echo $this->nvpstr;
			
			
			/* Make the API call to PayPal, using API signature.
			   The API response is stored in an associative array called $resArray */
			
			$this->resArray=hash_call("MassPay",$this->nvpstr);
			
			/* Display the API response back to the browser.
			   If the response from PayPal was a success, display the response parameters'
			   If the response was an error, display the errors received using APIError.php.
			   */
			   
			$ack = strtoupper($this->resArray["ACK"]);
			
			if($ack!="Sucess"){
					$_SESSION['reshash']=$this->resArray;
					$this->location = "APIError.php";
					header("Location: $this->location");
					
			   }
			   else {
			   	  QApplication::ExecuteJavaScript('opener.location.reload(true);self.window.close();');
			   }
        
			     	
        	
        	
        	
        	
        	
        	
        	
        	
        	/*//save data
            $bid_=new TableProjectBid();
            $bid_->Bid=intval($this->textbox_bid->Text);
            $bid_->DayToComplete=intval($this->textbox_delivery_within->Text);
            $bid_->Comment=$this->textbox_comment->Text;
            $bid_->set_bid_date_for_now();
            $bid_->set_status_active();
            $bid_->Enable='y';
            $bid_->BuyerId=$this->project->UserId;
            $bid_->UserId=$this->UserId;
            $bid_->ProjectId=$this->project->Id;
            $bid_->Save();*/

          
            $this->nvpstr='';
        }

        public function render_permission_error_text() {

            if(!$this->bool_permitted)
            {
                echo($this->str_permission_error_text);
            }
        }
        
        public function render_message_text() {

            if($this->bool_message_exist)
            {
                echo($this->str_message);
            }
        }

        public function render_confirming_text() {

            if($this->bool_confirming)
            {
                echo($this->str_confirming_text);
            }
        }

        public function set_error_message($str_) {

            if(!$this->bool_error)
            {
                $this->reset_message();
                $this->append_line_message($str_);
                $this->bool_error=true;
            }
        }

        public function reset_error_message() {
            $this->reset_message();
            $this->bool_error=false;
            $this->bool_message_exist=false;
        }

        public function append_permission_error_text($str_) {

            $this->str_permission_error_text.=$str_;
        }

        public function append_line_permission_error_text($str_) {

            $this->append_permission_error_text($str_.'<br />');
        }

        public function reset_permission_error_text() {

            $this->str_permission_error_text='';
        }

        public function append_message($str_) {

            $this->str_message.=$str_;
        }

        public function append_line_message($str_) {

            $this->append_message($str_.'<br />');
        }

        public function reset_message() {

            $this->str_message='';
        }

        public function append_confirming_text($str_) {

            $this->str_confirming_text.=$str_;
        }

        public function append_line_confirming_text($str_) {

            $this->append_confirming_text($str_.'<br />');
        }

        public function reset_confirming_text() {

            $this->str_confirming_text='';
        }

        public function bool_qcontrol_text_input_ok($qcontrol_) {

            if(isset($qcontrol_))
            {
                if (!($qcontrol_ instanceof QTextBoxBase))return false;

                if(strlen(trim($qcontrol_->Text))>0)
                {
                    $qcontrol_->Text=trim($qcontrol_->Text);
                    return true;
                }
            }
            return false;
        }
        

        public function bool_qcontrol_text_input_within($qcontrol_,$limit_) {

            if(isset($qcontrol_))
            {
                if (!($qcontrol_ instanceof QTextBoxBase))return false;

                if(strlen($qcontrol_->Text)<=$limit_)
                {
                    //$qcontrol_->Text=trim($qcontrol_->Text);
                    return true;
                }
            }
            return false;
        }

        public function bool_qcontrol_text_input_over($qcontrol_,$limit_) {

            if(isset($qcontrol_))
            {
                if (!($qcontrol_ instanceof QTextBoxBase))return false;

                if(strlen($qcontrol_->Text)>=$limit_)
                {
                    //$qcontrol_->Text=trim($qcontrol_->Text);
                    return true;
                }
            }
            return false;
        }

        protected function check_query_string()
        {
            if(is_numeric(QApplication::QueryString('id')))
            {
                $this->id=QApplication::QueryString('id');//project_id
            }
        }

        protected function load_project()
        {
            if(isset($this->id) && is_numeric($this->id))
            {
                $this->project=TableProject::LoadById($this->id);

                if(isset($this->project))
                {
                    $this->contents_html='project loaded-['.$this->project->Title.'] user_id=['.$this->project->UserId.']';
                }
                else
                {
                    $this->contents_html='project not found id=['.$this->id.']';
                    $this->id=null;
                    $this->project=null;
                }
            }
        }

        //check permissions
        protected function bool_project_loaded()
        {
            return isset($this->project);
        }

        protected function bool_my_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_my_project($this->UserId);
            }

            return false;
        }

        protected function bool_won_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_won_project($this->UserId);
            }
            
            return false;
        }

        protected function bool_already_bidding()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if(TableProject::bool_bidding_for_the_project($this->UserId,$this->id))
                {
                	if($this->bool_status())       
						{
						return false;
						}
                    return true;
                    
                }
            }

            return false;
        }

        protected function bool_already_won()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if(TableProject::bool_won_for_the_project($this->UserId,$this->id))
                {
                    return true;
                }
            }

            return false;
        }

        protected function bool_project_open()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_open_project();
            }

            return false;
        }
       protected function bool_status()
	{
		if(isset($this->project)&& isset($this->UserId) && isset($this->bid))
		{
			return $this->bid->bool_cancelled();
		}
		return false;

	}

        protected function bool_project_after_picked()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_after_picked_project();
            }

            return false;
        }

        protected function bool_project_bidding_end()
        {
            if(!bool_project_open() && !bool_project_after_picked())return true;

            return false;
        }
        
        //*_permitted is the convention
        protected function bool_bid_permitted()
        {
            if(!$this->bool_logined())
            {
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('You must login to access this page at least');
                return false;
            }
           /* if(!$this->bool_project_loaded())
            {
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project not found');
                return false;
            }
            if($this->bool_my_project())
            {
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project is yours');
                return false;
            }
            if($this->bool_already_bidding())
            {
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('You are already bidding the project');
                return false;
            }
            if($this->bool_already_won())
            {
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('You are already selected for the project');
                return false;
            }
            if(!$this->bool_project_open())
            {
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project is closed for bidding');
                return false;
            }*/

            //personal informaion & user level & bidding count etc
            //will also be checked for bidding after my_account_settting page is done.
           
            return true;
        }

        public function render_contents_area() {

            if($this->bool_contents_area_on)
            {
                echo($this->contents_html);
            }
        }
	
	}
	
	masspay::Run('masspay','masspay.tpl.php');
?>